package com.health.tools;

import com.github.promeg.pinyinhelper.Pinyin;


public class PinyinUtils {

    private static final String TAG = "PinyinUtils";

//    /**
//     * 获取拼音
//     *
//     * @param inputString
//     * @return
//     */
//    public static String getPingYin(String inputString) {
//        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//        format.setVCharType(HanyuPinyinVCharType.WITH_V);
//
//        char[] input = inputString.trim().toCharArray();
//        String output = "";
//
//        try {
//            for (char curChar : input) {
//                if (Character.toString(curChar).matches("[\\u4E00-\\u9FA5]+")) {
//                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(curChar, format);
//                    output += temp[0];
//                } else
//                    output += Character.toString(curChar);
//            }
//        } catch (BadHanyuPinyinOutputFormatCombination e) {
//            e.printStackTrace();
//        }
//        return output;
//    }

    public static String getPingYin2(String inputString) {

        char[] input = inputString.trim().toCharArray();
        String output = "";

        try {
            for (char curChar : input) {
                if (Character.toString(curChar).matches("[\\u4E00-\\u9FA5]+")) {
                    String temp = Pinyin.toPinyin(curChar);
                    output += temp;
                } else
                    output += Character.toString(curChar);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return output;
    }

    /**
     * 获取第一个字的拼音首字母
     * @param chinese
     * @return
     */
    public static String getFirstSpell(String chinese) {
        StringBuffer pinYinBF = new StringBuffer();
        char[] arr = chinese.toCharArray();
        for (char curChar : arr) {
            if (curChar > 128) {
                try {
                    String temp = Pinyin.toPinyin(curChar);
                    if (temp != null) {
                        pinYinBF.append(temp.charAt(0));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                pinYinBF.append(curChar);
            }
        }
        return pinYinBF.toString().replaceAll("\\W", "").trim();
    }


    public static void main(String[] args) {
        String py = getPingYin2("你好啊哈哈哈");
        System.out.println(py);

    }

}
